using System;
using Indiefreaks.Xna.Core;
using Indiefreaks.Xna.Logic;
using Indiefreaks.Xna.Rendering.Gui;
using Indiefreaks.Xna.Sessions;
using Microsoft.Xna.Framework.Content;

namespace Indiefreaks.AceOnSteroids.Menus
{
    /// <summary>
    /// The RootMainMenu is the Screen composed of all menu items at the root
    /// </summary>
    public class RootMainMenu : Screen, IContentHost
    {
        private readonly Button _exitMenuItem;
        private readonly Button _optionsMenuItem;
        private readonly Button _playMenuItem;

        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <remarks>
        /// We override the constructor so that it is always considered as a Menu screen
        /// We use the SessionManager to retrieve the current identified Player One (the one who hit "Start" on the PressStartLayer and pass its PlayerInput instance
        /// </remarks>
        public RootMainMenu()
            : base(true, true, Application.SunBurn.GetManager<SessionManager>(true).GetIdentifiedPlayer(LogicalPlayerIndex.One).Input)
        {
            // we create a first button that will let us move to the game itself
            _playMenuItem = new Button("Fonts/GuiFont", "Play") {X = 100, Y = 300}; // we position it at a fixed position

            // we catch the play button event to start a new game session
            _playMenuItem.Clicked +=new System.EventHandler(OnPlayMenuItemClicked);

            // we create an options button
            _optionsMenuItem = new Button("Fonts/GuiFont", "Options") {X = 100, Y = 348};
            // we then create a second button that will let us quit the game
            _exitMenuItem = new Button("Fonts/GuiFont", "Exit") {X = 100, Y = 396}; // we set the Y position depending on the previous menu item and the height of the SpriteFont used

            // for now, we'll simply hook the Exit menu item clicked event to quit the game
            _exitMenuItem.Clicked += delegate { Application.Instance.Exit(); };

            // we then add the menu items to the root main menu screen
            // the fonts will be loaded automatically when added.
            Add(_playMenuItem);
            Add(_optionsMenuItem);
            Add(_exitMenuItem);
        }

        private void OnPlayMenuItemClicked(object sender, EventArgs e)
        {
            // in here, we just have to ask the SessionManager to create a new session (this is a single player session since we are using the LocalSessionManager,
            // if you try to create another session type, you'll get an exception at runtime.
            // This won't happen if you use the LiveSessionManager for instance on Xbox or later the LidgrenSessionManager which will work on Windows.
            Application.SunBurn.GetManager<SessionManager>(true).CreateSinglePlayerSession();
        }

        #region Implementation of IContentHost

        public void LoadContent(IContentCatalogue catalogue, ContentManager manager)
        {
            // we add here to the catalogue (an IGF class) to add the button instances to the elements to load either synchronously or asynchronously depending on the
            // GameState loading system used
            catalogue.Add(_playMenuItem);
            catalogue.Add(_optionsMenuItem);
            catalogue.Add(_exitMenuItem);
        }

        public void UnloadContent(IContentCatalogue catalogue)
        {
        }

        #endregion
    }
}